//
//  FilterTeamViewController.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "FilterTeamViewController.h"
#import "FilterTeamTitleCell.h"

#import "filterTeamModel.h"


@interface FilterTeamViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;

/**
 *即时筛选
 */
@property(nonatomic,strong)NSMutableArray *timeFilterModelArray;

/**
 *赛果筛选
 */
@property(nonatomic,strong)NSMutableArray *resultFilterModelArray;

/**
 *最终筛选成功的
 */
@property(nonatomic,strong)NSMutableArray *filterFinishedModelArray;
@end

@implementation FilterTeamViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self customNavigationBar];
    
    
    
        UICollectionViewFlowLayout*flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenHeight - 64 - 49) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate=self;
    self.collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [self addButton];

   
    //传回去的数组
    self.filterFinishedModelArray = [NSMutableArray array];
    
    
    
    [self addTimeFilter];
    
    [self addResultFilter];
    
    
    if(self.timeTransferModelArray.count != 0)
    {
    
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [self  allSelectAction:nil];

        });
    }
    else
    {
        
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [self  allSelectAction:nil];
            
        });
        
        
    }
    
    
    
    
  //获取到上一次传过来的
    if(self.timeTransferModelArray.count!=0)
    {
        
           for (int i = 0; i < self.filterFirstModelArray.count; i++)
        {

            for(filterTeamModel *model in   self.timeFilterModelArray )
            {
                if([self.filterFirstModelArray[i] isEqualToString:model.title])
                {
                    model.isCleck = YES;
  
                }

            }        
 
        }
 
    }
    else
    {
        for (int i = 0; i < self.filterFirstModelArray.count; i++)
        {
            
            for(filterTeamModel *model in   self.resultFilterModelArray)
            {
                if([self.filterFirstModelArray[i] isEqualToString:model.title])
                {
                    model.isCleck = YES;
                    
                }
                
            }        
            
        }
      }
        
    
    
    
    
    
}

#pragma mark  将timeTransfer全部传给timeFilter
-(void)addTimeFilter
{
//timeTransfer是即时传过来的全部
self. timeFilterModelArray = [NSMutableArray array];

    for (int i = 0; i < self.timeTransferModelArray.count; i++)
    {
    filterTeamModel *model = [[filterTeamModel alloc]init];
    model.title = self.timeTransferModelArray[i];

    [self.timeFilterModelArray addObject:model];
    }
}
#pragma mark  将ResultTrans全部传给ResultFilter
-(void)addResultFilter
    {
        // //初始化resultFilterModelArray
        //resultTransfer  赛果传过来的全部
        self. resultFilterModelArray = [NSMutableArray array];
        
        for (int i = 0; i < self.resultTransferModelArray.count; i++)
        {
            filterTeamModel *model = [[filterTeamModel alloc]init];
            model.title = self.resultTransferModelArray[i];
            
            [self.resultFilterModelArray addObject:model];
        }
        
        
    }



#pragma mark - 自定义导航栏
- (void)customNavigationBar
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bg_green_2@2x"] forBarMetrics:UIBarMetricsDefault];
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"set_arrow_back"] style:UIBarButtonItemStylePlain target:self action:@selector(popback)];
    [backBarButton setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBarButton;
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenIp6(50), kScreenIp6(30))];
    title.textColor = [UIColor whiteColor];
    title.text = @"赛事筛选";
    [self.navigationItem setTitleView:title];
    
    
}

#pragma mark - 返回上级菜单按钮
- (void)popback
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 添加button
- (void)addButton
{
    //添加  全选,全不选, 确定按钮
    
    CGFloat selectViewY = kScreenHeight  -  49 - 64;
    CGFloat selectViewW = kScreenWidth;
    CGFloat selectViewH = 49;
    UIImageView *selectView = [[UIImageView alloc] initWithFrame:CGRectMake(0, selectViewY, selectViewW, selectViewH)];
    selectView.userInteractionEnabled = YES;
    selectView.image = [UIImage imageNamed:@"header_bg_green_2@2x"];
    
    CGFloat gap = kScreenIp6(20);
    CGFloat allSelectedW = (selectViewW - gap * 2) / 3;
    
    #pragma mark  全选
    UIButton *allSelected = [UIButton buttonWithType:UIButtonTypeCustom];
    allSelected.titleEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
    [allSelected setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [allSelected setTitle:@"全选" forState:UIControlStateNormal];
    allSelected.frame = CGRectMake(0, 0, allSelectedW, selectViewH);
    allSelected.showsTouchWhenHighlighted = YES;
    [allSelected addTarget:self action:@selector(allSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    [selectView addSubview:allSelected];
    
#pragma mark  全不选

    CGFloat noSelectedX = CGRectGetMaxX(allSelected.frame) + gap;
    UIButton *noSelected = [UIButton  buttonWithType:UIButtonTypeCustom];
    noSelected.frame = CGRectMake(noSelectedX, 0,allSelectedW , selectViewH);
    [noSelected setTitle:@"全不选" forState:UIControlStateNormal];
    [noSelected setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    noSelected.showsTouchWhenHighlighted = YES;
    [noSelected addTarget:self action:@selector(noOneSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    [selectView addSubview:noSelected];
    
#pragma mark  确定
    CGFloat saveX = CGRectGetMaxX(noSelected.frame) + gap;
    UIButton *save = [UIButton  buttonWithType:UIButtonTypeCustom];
    save.frame = CGRectMake(saveX, 0,allSelectedW , selectViewH);
    [save setTitle:@"确定" forState:UIControlStateNormal];
    save.showsTouchWhenHighlighted = YES;
    [save addTarget:self action:@selector(sureButttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [save setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [selectView addSubview:save];
    
    [self.view addSubview:selectView];
    
}













#pragma mark  全选  按钮点击
-(void)allSelectAction:(UIButton*)sender
{
    if(self.timeTransferModelArray.count != 0)
    {
        [self addTimeFilter];
        
        
        for (filterTeamModel *model in self.timeFilterModelArray)
        {
            model.isCleck =YES;
        }
        
        
        [self.collectionView reloadData];
        
    }
    else
    {
        [self addResultFilter];
        
        for (filterTeamModel *model in self.resultFilterModelArray)
        {
            model.isCleck =YES;
        }
        
      
        
        [self.collectionView reloadData];
        
    }
    
    
    
}


#pragma mark  全不选按钮
-(void)noOneSelectAction:(UIButton*)sender
{
    
    if(self.timeTransferModelArray.count != 0)
    {
        
        
        [self addTimeFilter];
        
        for (filterTeamModel *model in self.timeFilterModelArray)
        {
            model.isCleck =NO;
        }
        
        
        
         [self.collectionView reloadData];
    }
    else
    {
        [self addResultFilter];
        
        for (filterTeamModel *model in self.resultFilterModelArray)
        {
            model.isCleck = NO;
        }
        
        
        
         [self.collectionView reloadData];
        
    }
    
}
#pragma mark  确定按钮
-(void)sureButttonAction:(UIButton*)sender
{
    
    if(self.timeTransferModelArray.count != 0)
    {
        
        for (filterTeamModel *model in self.timeFilterModelArray)
        {
            if (model.isCleck)
            {
                [self.filterFinishedModelArray addObject:model.title];
            }
            
        }
        
        //        [[NSUserDefaults standardUserDefaults]setObject:self.filterFinishedModelArray forKey:@"filterTime"];
        
        
#pragma mark  修改后的block
        if(self.block != nil)
        {
            
            self.block(self.filterFinishedModelArray);
        }
        
        
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
    else
    {
        for (filterTeamModel *model in self.resultFilterModelArray)
        {
            if (model.isCleck)
            {
                [self.filterFinishedModelArray addObject:model.title];
            }
            
        }
        
        ////       [[NSUserDefaults standardUserDefaults]setObject:self.filterFinishedModelArray forKey:@"filterResult"];
        //
        //
        //#pragma mark  修改后的block
        if(self.block != nil)
        {
            
            self.block(self.filterFinishedModelArray);
        }
        
        
        [self.navigationController popViewControllerAnimated:YES];
        
        
    }
    
    
}



#pragma mark  代理方法

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (self.timeTransferModelArray.count != 0)
    {
        return self.timeFilterModelArray.count;
    }
    else
    {
        return self.resultFilterModelArray.count;
    }
    
    
    
}




-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.timeTransferModelArray.count != 0)
    {
        static NSString *cellIndentifier = @"collectionCell";
        
        [collectionView registerClass:[FilterTeamTitleCell class] forCellWithReuseIdentifier:cellIndentifier];
        FilterTeamTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifier forIndexPath:indexPath];
        
        
        filterTeamModel *model = self.timeFilterModelArray[indexPath.row];
        
        
        for(NSString *str in self.timeFilterModelArray)
        {
            if([model.title isEqualToString:str])
            {
                model.isCleck = YES;
            }
            
        }
        
        
        
        if(model.isCleck ==YES)
        {
            
            cell.label.backgroundColor = [UIColor orangeColor];
            
        }
        else
        {
            
            cell.label.backgroundColor = [UIColor grayColor];
            
        }
        
        
        cell.label.text = model.title;
        
        return cell;
        
        
        
    }
    
    else
    {
        
        
        static NSString *cellIndentifier = @"collectionCell";
        
        [collectionView registerClass:[FilterTeamTitleCell class] forCellWithReuseIdentifier:cellIndentifier];
        FilterTeamTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifier forIndexPath:indexPath];
        
        
        filterTeamModel *model = self.resultFilterModelArray[indexPath.row];
        
        
        
        for(NSString *str in self.resultFilterModelArray)
        {
            if([model.title isEqualToString:str])
            {
                model.isCleck = YES;
            }
            
        }
        
        
        
        if(model.isCleck ==YES)
        {
            
            cell.label.backgroundColor = [UIColor orangeColor];
            
        }
        else
        {
            
            cell.label.backgroundColor = [UIColor grayColor];
            
        }
        
        
        cell.label.text = model.title;
        
        return cell;

    }
    
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
        
    if (self.timeTransferModelArray.count != 0)
    {
   
        
       
        
        
        FilterTeamTitleCell *cell = (FilterTeamTitleCell*)[collectionView cellForItemAtIndexPath:indexPath];
        
        filterTeamModel *model = self.timeFilterModelArray[indexPath.row];
        model.isCleck =!model.isCleck;
        
        if (model.isCleck)
        {
            cell.label.backgroundColor = [UIColor orangeColor];
            
        }
        else
        {
            cell.label.backgroundColor = [UIColor grayColor];
            
        }
        
        
    }
    else
    {
        
        
        FilterTeamTitleCell *cell = (FilterTeamTitleCell*)[collectionView cellForItemAtIndexPath:indexPath];
        
        filterTeamModel *model = self.resultFilterModelArray[indexPath.row];
        model.isCleck =!model.isCleck;
        
        if (model.isCleck)
        {
            cell.label.backgroundColor = [UIColor orangeColor];
            
        }
        else
        {
            cell.label.backgroundColor = [UIColor grayColor];
            
        }
        
    }
    
    
    
}



-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake((kScreenHeight - 64 - 49 - kScreenIp6(100)* 4 ) / 5, (kScreenWidth - kScreenIp6(100) * 3) / 4, (kScreenHeight - 64 - 49 - kScreenIp6(100)* 4 ) / 5, (kScreenWidth - kScreenIp6(100) * 3) / 4);
}

//// 最小列距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return (kScreenWidth - kScreenIp6(100) * 3) / 4;
}
// 行距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return (kScreenHeight - 64 - 49 - kScreenIp6(100)* 4 ) / 5;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(kScreenIp6(100), kScreenIp6(100));
}



#pragma mark - 隐藏和显示状态栏
- (void)viewWillAppear:(BOOL)animated
{
      [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}


- (void)viewWillDisappear:(BOOL)animated
{
      [super viewWillDisappear:animated];    
    self.tabBarController.tabBar.hidden = NO;
}



@end
